package cn.snmp.mutual.data;

import java.util.HashMap;
import java.util.LinkedList;
/**
 * @since 2018.9.16
 * @author dengkn
 * XML 解析后存放的容器，用来存放Xml信息层级关系及所有信息
 * 数据在读取的时候需要一层层的剥开
 */
public class XmlService implements Cloneable {
	public static final short ATTRIBUTE_NODE = 2;
	public static final short CDATA_SECTION_NODE = 4;
	public static final short COMMENT_NODE = 8;
	public static final short DOCUMENT_FRAGMENT_NODE = 11;
	public static final short DOCUMENT_NODE = 9;
	public static final short DOCUMENT_TYPE_NODE = 10;
	public static final short ELEMENT_NODE = 1;
	public static final short ENTITY_NODE = 6;
	public static final short ENTITY_REFERENCE_NODE = 5;
	public static final short NOTATION_NODE = 12;
	public static final short PROCESSING_INSTRUCTION_NODE = 7;
	public static final short TEXT_NODE = 3;
	public static final String XML_PRAMETER_TESX = "#text";
	public static final String XML_PRAMETER_COMMENT = "#comment";
	
	private String mNodeName = "";
	private int mNodeType = 0;
	private HashMap xmlHashMap = new HashMap();
	private LinkedList xmlVector = new LinkedList();
	private int mNodeLevel;

	public XmlService() {
		this.mNodeLevel = 1;
	}

	public XmlService(String className) {
		this.mNodeName = className;
		this.mNodeLevel = 1;
	}

	public LinkedList getCurrentVector() {
		return this.xmlVector;
	}

	private String getNodeTypeString(short nodeType) {
		switch (nodeType) {
		case 1:
			return "ELEMENT_NODE";
		case 2:
			return "ATTRIBUTE_NODE";
		case 3:
			return "TEXT_NODE";
		case 4:
			return "CDATA_SECTION_NODE";
		case 5:
			return "ENTITY_REFERENCE_NODE";
		case 6:
			return "ENTITY_NODE";
		case 7:
			return "PROCESSING_INSTRUCTION_NODE";
		case 8:
			return "COMMENT_NODE";
		case 9:
			return "DOCUMENT_NODE";
		case 10:
			return "DOCUMENT_TYPE_NODE";
		case 11:
			return "DOCUMENT_FRAGMENT_NODE";
		case 12:
			return "NOTATION_NODE";
		default:
			return "Unknown node type, error";
		}
	}

	public XmlService(String className, HashMap hashMap) {
		if (className != null) {
			this.mNodeName = className;
		}

		this.xmlHashMap = (HashMap) hashMap.clone();
		this.mNodeLevel = 1;
	}

	public XmlService(XmlService xmlService) {
		if (xmlService.getNodeName() != null) {
			this.mNodeName = xmlService.getNodeName();
		} else {
			this.mNodeName = "";
		}

		if (xmlService.getNodeType() != 0) {
			this.mNodeType = xmlService.getNodeType();
		} else {
			this.mNodeType = 0;
		}

		if (xmlService.getCurrentHashMap() != null) {
			this.xmlHashMap = (HashMap) xmlService.getCurrentHashMap().clone();
		} else {
			this.xmlHashMap = new HashMap();
		}

		if (xmlService.getCurrentVector() != null) {
			this.xmlVector = (LinkedList) xmlService.getCurrentVector().clone();
		} else {
			this.xmlVector = new LinkedList();
		}

		this.mNodeLevel = 1;
	}

	public int getNodeLevel() {
		return this.mNodeLevel;
	}

	public void setNodeLevel(int nodeLevel) {
		this.mNodeLevel = nodeLevel;
	}

	public int getNodeType() {
		return this.mNodeType;
	}

	public void setNodeType(int nodeType) {
		this.mNodeType = nodeType;
	}

	public String getNodeName() {
		return this.mNodeName != null ? this.mNodeName : "XmlService";
	}

	public void setNodeName(String nodeName) {
		this.mNodeName = nodeName;
	}

	public HashMap getCurrentHashMap() {
		return this.xmlHashMap;
	}

	public void setCurrentHashMap(HashMap hashMap) {
		this.xmlHashMap = hashMap;
	}

	public void addContainedObject(XmlService xmlService) {
		if (xmlService != null) {
			this.xmlVector.add(xmlService);
		}
	}

	public void removeContainedObject(int index) {
		if (index < this.xmlVector.size()) {
			this.xmlVector.remove(index);
		}

	}

	public int columnSize() {
		return this.xmlVector.size();
	}

	public XmlService objectAt(int index) {
		return index >= this.xmlVector.size() ? null : (XmlService) this.xmlVector.get(index);
	}

	public LinkedList getAllContainedObject() {
		return this.xmlVector;
	}

	public Object getValue(String key) {
		Object obj = null;
		if (this.xmlHashMap == null) {
			return obj;
		} else {
			obj = this.xmlHashMap.get(key);
			return obj;
		}
	}

	public String getStringValue(String key) {
		Object obj = this.getValue(key);
		return obj == null ? "" : obj.toString();
	}

	public int getIntValue(String key) {
		Object obj = this.getValue(key);
		return obj == null ? 0 : Integer.parseInt(obj.toString());
	}

	public long getLongValue(String key) {
		Object obj = this.getValue(key);
		return obj == null ? 0L : Long.parseLong(obj.toString());
	}

	public double getDoubleValue(String key) {
		Object obj = this.getValue(key);
		return obj == null ? 0.0D : Double.parseDouble(obj.toString());
	}

	public void setValue(String key, Object obj) {
		if (this.xmlHashMap == null) {
			this.xmlHashMap = new HashMap();
		}

		try {
			this.xmlHashMap.put(key, obj.toString());
		} catch (Exception arg3) {

		}
	}

	public void setIntValue(String key, int i) {
		this.setValue(key, String.valueOf(i));
	}

	public void setLongValue(String key, long l) {
		this.setValue(key, String.valueOf(l));
	}

	public void setDoubleValue(String key, double d) {
		this.setValue(key, String.valueOf(d));
	}

	public void clearValue() {
		this.xmlHashMap.clear();
	}

	public void dumpData() {
		for (int i = 0; i < this.xmlVector.size(); ++i) {
			XmlService xmlService = (XmlService) this.xmlVector.get(i);
			xmlService.dumpData();
		}
	}
	
	@Override
	public Object clone() {
		String nodeName = null;
		HashMap hashMap = null;
		LinkedList vector = null;
		if (this.mNodeName != null) {
			nodeName = this.mNodeName;
		}

		if (this.xmlHashMap != null) {
			hashMap = (HashMap) this.xmlHashMap.clone();
		}

		if (this.xmlVector != null) {
			vector = (LinkedList) this.xmlVector.clone();
		}
		XmlService xmlService = new XmlService(nodeName, hashMap);
		xmlService.setNodeType(this.mNodeType);
		xmlService.setNodeLevel(this.mNodeLevel);
		if (vector != null) {
			for (int i = 0; i < vector.size(); ++i) {
				XmlService clildXmlService = (XmlService) vector.get(i);
				xmlService.addContainedObject((XmlService) clildXmlService.clone());
			}
		}
		return xmlService;
	}

	public static XmlService objService2XmlService(ObjService objService) {
		ObjService cloneobj = objService.clone();
		XmlService xmlService = new XmlService(cloneobj.getClsName(), cloneobj.getCurrentHashMap());
		xmlService.setNodeType(1);
		if (XML_PRAMETER_TESX.equals(cloneobj.getClsName())) {
			xmlService.setNodeType(3);
		}

		if (XML_PRAMETER_COMMENT.equals(cloneobj.getClsName())) {
			xmlService.setNodeType(8);
		}

		LinkedList vector = cloneobj.getObjLinkListVecs();
		int vectorSize = vector.size();

		for (int i = 0; i < vectorSize; ++i) {
			LinkedList childVector = (LinkedList) vector.get(i);
			int childVectorSize = childVector.size();

			for (int j = 0; j < childVectorSize; ++j) {
				ObjService childObjService = (ObjService) childVector.get(j);
				XmlService childXmlService = objService2XmlService(childObjService);
				childXmlService.setNodeType(1);
				if (XML_PRAMETER_TESX.equals(childObjService.getClsName())) {
					childXmlService.setNodeType(3);
				}
				if (XML_PRAMETER_COMMENT.equals(childObjService.getClsName())) {
					childXmlService.setNodeType(8);
				}

				xmlService.addContainedObject(childXmlService);
			}
		}
		return xmlService;
	}

	public static ObjService xmlService2ObjService(XmlService xmlService) {
		XmlService cloneXmlObj = (XmlService) xmlService.clone();
		ObjService objService = new ObjService(cloneXmlObj.getNodeName(), cloneXmlObj.getCurrentHashMap());
		LinkedList vector = cloneXmlObj.getCurrentVector();
		int vectorSize = vector.size();

		for (int i = 0; i < vectorSize; ++i) {
			XmlService childXmlService = (XmlService) vector.get(i);
			ObjService childObjService = xmlService2ObjService(childXmlService);
			objService.addContainedObject(childObjService);
		}

		return objService;
	}

	public void remove(String nodeName) {
		for (int i = 0; i < this.xmlVector.size(); ++i) {
			XmlService xmlService = (XmlService) this.xmlVector.get(i);
			String childNodeName = xmlService.getNodeName();
			if (childNodeName.equals(nodeName)) {
				this.xmlVector.remove(xmlService);
			}
		}

	}

	public void remove(int index) {
		if (index <= this.xmlVector.size()) {
			this.xmlVector.remove(index);
		}
	}

	public void reset() {
		this.xmlHashMap.clear();
		this.mNodeName = null;
		this.mNodeType = 0;
		this.xmlVector.clear();
	}
}